import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class BoardAWT extends Panel implements MouseListener
{
	private static final long serialVersionUID = 1L;
	private HolesAWT[][] holes = new HolesAWT[12][12];
	private int player = 1;
	//private AI ai = new AI();
	private Game game = new Game();
	private int mode = 2;
	
	public Game getGame() 
	{
		return game;
	}

	public HolesAWT[][] getHoles() 
	{
		return holes;
	}
	
	/**set the order of the players
	 * @param player can be 1 or 2**/
	public void setPlayer(int player) 
	{
		this.player = player;
	}

	/**gets the actual player**/
	public int getPlayer() 
	{
		return player;
	}

	public void setMode(int mode) 
	{
		this.mode = mode;
	}

	public int getMode() 
	{
		return mode;
	}
	
	/**class constructor**/
	public BoardAWT()
	{
		this.setLayout(null);
		init();
	}

	/**board initializing**/
	public void init()
	{
		removeAll();
		setPlayer(1);
		int y = 60;
		for (int i = 1; i<11;i++)
		{
			int x = 10;
			for (int j = 1; j<11; j++)
			{
				holes[i][j] = new HolesAWT();
				holes[i][j].addMouseListener((MouseListener) this);
				holes[i][j].setIs(0);
				//holes[i][j].setCol(Color.WHITE);
				holes[i][j].setImgstr("res\\hole.png");
				holes[i][j].setBounds(x, y, 30, 30);
				add(holes[i][j]);
				//holes[i][j].repaint();
				holes[i][j].setColumn(j);
				x += 35;
			}
			y += 35;
		}
		for (int i=0; i<12;i++) //strazsa sorok es oszlopok
		{
			holes[0][i] = new HolesAWT();
			holes[0][i].setIs(3);
			holes[i][0] = new HolesAWT();
			holes[i][0].setIs(3);
			holes[11][i] = new HolesAWT();
			holes[11][i].setIs(3);
			holes[i][11] = new HolesAWT();
			holes[i][11].setIs(3);
		}
		//System.out.println("Board initialized");
	}

	/**MouseListener mousePressed method**/
	public void mousePressed(MouseEvent e)
	{
		try
		{
			HolesAWT source = (HolesAWT) e.getSource();
			game.start(source.getColumn());
		}
		catch (ArrayIndexOutOfBoundsException ex)
		{
			//System.out.println("kivetel");
			ex.printStackTrace();
			Toolkit.getDefaultToolkit().beep();
		}
	}
	
	public void paint(Graphics g)
	{
		BufferedImage bg = null;
		try 
		{
			bg = ImageIO.read(new File("res\\gameboard.png"));
			g.drawImage(bg, 0, 50, this);
		} 
		catch (IOException e) {e.printStackTrace();}
	}
	public void mouseClicked(MouseEvent e){}

	public void mouseEntered(MouseEvent e) {}

	public void mouseExited(MouseEvent e) {}

	public void mouseReleased(MouseEvent e) {}

	
}
